package com.starhub.application.collection.common;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starhub.application.collection.dto.MetaDataField;
import com.starhub.application.collection.entity.KnowledgeCollection;
import com.starhub.application.collection.entity.MetadataRelation;
import com.starhub.application.collection.enums.CollectionTypeEnums;
import com.starhub.application.collection.service.KnowledgeCollectionService;
import com.starhub.application.rag.constant.RagConstant;
import com.starhub.utils.Util;

/**
 * 元数据关联字段处理类
 * 
 * @version: V1.0
 * @author: liuhf
 * @time 2025-06-09
 */
@Component
public class MetaDataLinkHelper {

    @Autowired
    private KnowledgeCollectionService knowledgeCollectionService;

    /**
     * 获取元数据关联字段
     * @param allRelations 关联数据列表
     * @param collectionId 知识库表ID
     * @return 元数据关联字段
     */
    public MetaDataField getMetaDataLinkField(List<MetadataRelation> relations, String collectionName) {
        /*KnowledgeCollection knowledgeCollection = knowledgeCollectionService
                .selectById(Util.getLongValue(collectionId));
        if (knowledgeCollection == null) {
            throw new RuntimeException("知识库表不存在");
        }
        if (knowledgeCollection.getBusinessType() != CollectionTypeEnums.META_DATA.getCode()) {
            throw new RuntimeException("知识库表类型错误");
        }*/

        // TODO 不同的类型执行不同的构建逻辑

        // 根据数据类型匹配返回内容
        return MetaDataField.builder()
                .fieldName(RagConstant.META_PREFIX + collectionName)
                .fieldValue(relations == null ? "" : relations.stream().map(MetadataRelation::getMetaDataId).collect(Collectors.joining(",")))
                .build();
    }

}
